package com.jie.langlangmall.controller;

import com.jie.langlangmall.common.api.CommonPage;
import com.jie.langlangmall.common.api.CommonResult;
import com.jie.langlangmall.common.constant.StatusConstant;
import com.jie.langlangmall.model.UmsResource;
import com.jie.langlangmall.security.component.DynamicSecurityMetadataSource;
import com.jie.langlangmall.service.UmsResourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Jie
 * @Date 2024/11/22 16:42
 * @Email 3217998214@qq.com
 * @Description 后台资源管理
 */
@RestController
@Tag(name = "UmsResourceController", description = "后台资源管理")
@RequestMapping("/resource")
public class UmsResourceController {

    @Autowired
    private UmsResourceService resourceService;

    @Autowired
    private DynamicSecurityMetadataSource dynamicSecurityMetadataSource;

    @Operation(summary = "查询所有后台资源")
    @GetMapping("/listAll")
    public CommonResult<List<UmsResource>> listAll() {
        List<UmsResource> resourceList = resourceService.listAll();
        return CommonResult.success(resourceList);
    }

    @Operation(summary = "分页查询资源")
    @GetMapping("/list")
    public CommonResult<CommonPage<UmsResource>> list(@RequestParam(required = false) Long categoryId,
                                                      @RequestParam(required = false) String nameKeyword,
                                                      @RequestParam(required = false) String urlKeyword,
                                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<UmsResource> resourceList = resourceService.list(categoryId, nameKeyword, urlKeyword, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(resourceList));
    }

    @Operation(summary = "添加后台资源")
    @PostMapping("/create")
    public CommonResult<Integer> create(@RequestBody UmsResource umsResource) {
        // 添加资源
        int count = resourceService.create(umsResource);
        if (count == StatusConstant.SUCCESS) {
            dynamicSecurityMetadataSource.clearDataSource();
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @Operation(summary = "修改后台资源")
    @PostMapping("/update/{id}")
    public CommonResult<Integer> update(@PathVariable("id") Long id,
                                        @RequestBody UmsResource umsResource) {
        int count = resourceService.update(id, umsResource);
        if (count == StatusConstant.SUCCESS) {
            dynamicSecurityMetadataSource.clearDataSource();
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @Operation(summary = "根据ID获取资源详情")
    @GetMapping("/{id}")
    public CommonResult<UmsResource> getItem(@PathVariable("id") Long id) {
        UmsResource umsResource = resourceService.getItem(id);
        return CommonResult.success(umsResource);
    }

    @Operation(summary = "根据ID删除后台资源")
    @PostMapping("/delete/{id}")
    public CommonResult<Integer> delete(@PathVariable("id") Long id) {
        int count = resourceService.delete(id);
        if (count == StatusConstant.SUCCESS) {
            dynamicSecurityMetadataSource.clearDataSource();
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

}
